package com.bulk.demo.insert.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bulk.demo.insert.entity.Student;
import com.bulk.demo.insert.mapper.StudentMapper;
import com.bulk.demo.insert.service.IStudentService;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 散装java
 * @date 2022-09-13
 */
@Service
@Primary
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Resource
    StudentMapper studentMapper;
    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public int mySaveBatch(List<Student> list) {
        return studentMapper.mySaveBatch(list);
    }

    @SneakyThrows
    @Override
    public int saveBatchByJDBC(List<Student> list) {
        String sql = "INSERT INTO student (name,sex,tel,email,address,remark,add_time,update_time,column10,column11,column12,column13,column14,column15,column16,column17,column18,column19,column20,column21,column22,column23,column24,column25,column26,column27,column28,column29,column30) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = jdbcTemplate.getDataSource().getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                addPs(ps, student);
                ps.addBatch();

                if (i % 1000 == 0 || i == list.size() - 1) {
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            assert conn != null;
            conn.setAutoCommit(true);
            JdbcUtils.closeConnection(conn);
            JdbcUtils.closeStatement(ps);
        }
        return 0;
    }

    private void addPs(PreparedStatement ps, Student student) throws SQLException {
        ps.setObject(1, student.getName());
        ps.setObject(2, student.getSex());
        ps.setObject(3, student.getTel());
        ps.setObject(4, student.getEmail());
        ps.setObject(5, student.getAddress());
        ps.setObject(6, student.getRemark());
        ps.setObject(7, student.getAddTime());
        ps.setObject(8, student.getUpdateTime());
        ps.setObject(9, student.getColumn10());
        ps.setObject(10, student.getColumn11());
        ps.setObject(11, student.getColumn12());
        ps.setObject(12, student.getColumn13());
        ps.setObject(13, student.getColumn14());
        ps.setObject(14, student.getColumn15());
        ps.setObject(15, student.getColumn16());
        ps.setObject(16, student.getColumn17());
        ps.setObject(17, student.getColumn18());
        ps.setObject(18, student.getColumn19());
        ps.setObject(19, student.getColumn20());
        ps.setObject(20, student.getColumn21());
        ps.setObject(21, student.getColumn22());
        ps.setObject(22, student.getColumn23());
        ps.setObject(23, student.getColumn24());
        ps.setObject(24, student.getColumn25());
        ps.setObject(25, student.getColumn26());
        ps.setObject(26, student.getColumn27());
        ps.setObject(27, student.getColumn28());
        ps.setObject(28, student.getColumn29());
        ps.setObject(29, student.getColumn30());
    }
}
